import api from '../../utils/api'
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({
    data: {
        array: [],
        index: '',
        username: '',
        phone: '',
        imgUrl: config.imgUrl,
    },
    wacthName(e) {
        this.setData({
            username: e.detail.value
        })
    },
    wacthPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    submit() {
        let username = this.data.username
        let phone = this.data.phone
        let area_id = this.data.newArr[this.data.index2]
        if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
            wx.showModal({
                title: '提示',
                content: '你输入的电话不符，请重新检查填写',
                showCancel: false
            })
        } else {
            if (username && phone && area_id) {
                http.request({
                    url: '/shopSettlein',
                    header: {
                        'token': wx.getStorageSync("token")
                    },
                    data: {
                        username,
                        phone,
                        city_id: area_id.city_id
                    },
                    success: res => {
                        if (res.code == 200) {
                            wx.showModal({
                                content: '稍后会有客服人员联系您，\r\n请保持电话通畅。',
                                showCancel: false,
                                confirmColor: '#EA5D56',
                                success: function (e) {
                                    if (e.confirm) {
                                        wx.navigateBack({})
                                    }
                                }
                            })
                        } else {
                            this.setData({
                                error: res.message
                            })
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '请输入完整信息',
                    icon: 'none',
                    mask: true
                })
            }
        }
    },
    bindPickerChange: function (e) {
        this.setData({
            index: e.detail.value,
            isDis: false,
            index2: 0,
            newArr: this.data.allArr[e.detail.value],
        })
        let newName = []
        let sur = []
        this.data.newArr.forEach((item, index) => {
            newName.push(item.name)
            sur.push(item.number)
        })
        this.setData({
            newName,
            sur
        })
    },

    bindPickerChange2: function (e) {
        this.setData({
            index2: e.detail.value,
        })
    }, // 获取招商区域
    diqu() {
        http.request({
            url: api.getDizhi,
            success: res => {
                let array = []
                let allArr = []
                res.data.forEach((item, index) => {
                    array.push(item.name)
                    allArr.push(item.list)
                })
                this.setData({
                    array,
                    allArr
                })
            },
        })
    },
    onLoad: function (options) {
        this.diqu()
    }
})